package com.hemerasolutions.v2trainer.model.exercise.intensity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import com.hemerasolutions.v2trainer.model.base.BaseModel;
import com.hemerasolutions.v2trainer.model.exercise.executions.Execution;
import com.hemerasolutions.v2trainer.model.modality.Modality;

@Entity
public class IntensityModTable extends BaseModel {

	private static final long serialVersionUID = 1L;

    @ManyToOne
    private Modality modality;

    @ManyToOne
    private Execution execution;
    
    @OneToMany( mappedBy="intensityModTable" ,cascade=CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval=true)
    private List<IntensityModZone> intensityModZones;

    
	public IntensityModTable() {
		super();
		this.intensityModZones = new ArrayList<IntensityModZone>();
	}

	public IntensityModTable(IntensityModTable other, Modality modality){
		this();
		this.modality = modality;
		this.execution = other.execution;
		
		for(IntensityModZone imz:other.intensityModZones){
			this.intensityModZones.add(new IntensityModZone(imz,this));
		}
		
	}	

	public Execution getExecution() {
		return execution;
	}

	public void setExecution(Execution execution) {
		this.execution = execution;
	}

	public Modality getModality() {
		return modality;
	}

	public void setModality(Modality modality) {
		this.modality = modality;
	}

	public List<IntensityModZone> getIntensityModZones() {
		return intensityModZones;
	}

	public List<IntensityZone> getIntensityZones(IntensityTable it){
		List<IntensityZone> izs = new ArrayList<IntensityZone>();
		for(IntensityModZone imz:this.intensityModZones){
			izs.add(new IntensityZone(imz,it));
		}
		return izs;
	}
	
	public void setIntensitysModZones(List<IntensityModZone> intensityZones) {
		this.intensityModZones = intensityZones;
	}



	
	public void upIntensityModZone(IntensityModZone iz){
		
		int i = intensityModZones.indexOf(iz);
		if(iz.getPosition() != 0 ){
			intensityModZones.add(i - 1,intensityModZones.remove(i));	
		}

		this.refresh();
	}

	public void downIntensityModZone(IntensityModZone iz){

		int i = intensityModZones.indexOf(iz);
		if(iz.getPosition() < intensityModZones.size() - 1){
			intensityModZones.add(i + 1,intensityModZones.remove(i));	
		}

		this.refresh();
	}	
	
	
	public IntensityModZone addIntensityModZone(){

		this.intensityModZones.add(new IntensityModZone(this));
		return this.intensityModZones.get(this.intensityModZones.size() - 1);
		
	}
	
	
	public void refresh() {
		for(int i = 0; i < intensityModZones.size(); i++ ){
			IntensityModZone iz = intensityModZones.get(i);
			iz.setPosition(i);
		}
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((execution == null) ? 0 : execution.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((modality == null) ? 0 : modality.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		IntensityModTable other = (IntensityModTable) obj;
		if (execution == null) {
			if (other.execution != null) {
				return false;
			}
		} else if (!execution.equals(other.execution)) {
			return false;
		}
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (modality == null) {
			if (other.modality != null) {
				return false;
			}
		} else if (!modality.equals(other.modality)) {
			return false;
		}
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "IntensityModTable [id=" + id + ", modality=" + modality
				+ ", execution=" + execution + "]";
	}


	

}

